package asyncsenders;

import dataobjects.Invite;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;
import flex.messaging.services.MessageService;
import flex.messaging.services.ServiceAdapter;
import flex.messaging.util.UUIDUtils;

public class InvitesSender extends ServiceAdapter {
	
	public void sendInvite(String toUser){
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("InvitesPush");
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setHeader("toUser", toUser);
		msg.setHeader("info", "new invite");
		msg.setBody("");
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		msgBroker.routeMessageToService(msg, null);

	}
	
	public void declineTrade(String askUsername, String recieveUsername, String givenCardName, String wantedCardName){
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("InvitesPush");
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setHeader("toUser", askUsername);
		msg.setHeader("info", "trade declined");
		String body = "Trade with "+recieveUsername+"\n on "+givenCardName+" and "+wantedCardName+"\n DECLINED.";
		msg.setBody(body);
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		msgBroker.routeMessageToService(msg, null);
	}
	
	public void declineGame(String askUsername){
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("InvitesPush");
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setHeader("toUser", askUsername);
		msg.setHeader("info", "game declined");
		String body = "";
		msg.setBody(body);
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		msgBroker.routeMessageToService(msg, null);
	}
	
	public void confirmTrade(String askUsername, String recieveUsername, String givenCardName, String wantedCardName){
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("InvitesPush");
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setHeader("toUser", askUsername);
		msg.setHeader("info", "trade confirmed");
		String body = "Trade with "+recieveUsername+"\n on "+givenCardName+" and "+wantedCardName+"\n CONFIRMED.";
		msg.setBody(body);
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		msgBroker.routeMessageToService(msg, null);
	}

	@Override
	public Object invoke(Message message) {
		AsyncMessage newMessage = (AsyncMessage) message;
		MessageService msgService = (MessageService) getDestination().getService();
		msgService.pushMessageToClients(newMessage, true);
		return null;
	}

	public void confirmGame(Invite invite) {
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("InvitesPush");
		msg.setMessageId(UUIDUtils.createUUID());
		msg.setHeader("toUser", invite.askingUsername);
		msg.setHeader("info", "game confirmed");
		msg.setBody(invite);
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		msgBroker.routeMessageToService(msg, null);		
	}

}
